<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>WebSocket Chat</title>
        <script type='text/javascript'>
            if (!window.WebSocket)
                alert("WebSocket not supported by this browser");
            var room = {
                join: function(storeId, tableName, username) {
                    this._storeId = storeId;
                    this._username = username;
                    this._tableName = tableName;
                    var location = "ws://<%=request.getServerName()%>:8088/WebSocket/websocket";
                    this._ws=new WebSocket(location);
                    this._ws.onopen=this._onopen;
                    this._ws.onmessage=this._onmessage;
                    this._ws.onclose=this._onclose;
                },
        
                _onopen: function(){
                    var command = { 
                        command: "join", 
                        storeId: room._storeId,
                        tableName: room._tableName,
                        username: room._username 
                    };
                    room._send(JSON.stringify(command));
                },
        
                _send: function(message){
                    alert("room._send() message = "+message);
                    if (this._ws)
                        this._ws.send(message);
                },
      
                chat: function(text) {
                    if (text != null && text.length>0 )
                        room._send(room._username,text);
                },
        
                _onmessage: function(m) {
                   var json = eval("("+m.data+")");
                   if("joinDone" == json.command){
                        //加入成功则请求已点菜单
                        room._send(JSON.stringify({
                            command: "orderList"
                        }));
                   } else if("orderList" == json.command){
                       alert("orderList response message.data = " + m.data);
                   } else if("dish" == json.command){
                       alert("dish response message.data = " + m.data);
                   }else{
                       alert("Unkown message from server. message.data = " + m.data);
                   }
                },
        
                _onclose: function(m) {
                    this._ws=null;
                }
        
            };
            //room.join(5, "5号桌", "mellon");
            function jonTable(storeId, tableName, userName){
                room.join(storeId, tableName, userName);
            }
            function orderDish(){
                room._send(JSON.stringify({
                    command: "dish",
                    menuId: 1,
                    courseId: 5
                })); 
            }
      
        </script>
    </head>
    <body>
        <input type="button" onclick="javascript: orderDish();" value="order"/>
    </body>
</html>